package com.letv.whatslive.model.convert;

import com.letv.whatslive.model.AutoTask;
import com.letv.whatslive.model.utils.Constant;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * Created by wangjian7 on 2016/3/1.
 */
public class AutoTaskConvert {
    public static DBObject castAutoTaskConvertToDBObject(AutoTask autoTask){
        DBObject dbo = new BasicDBObject();
        if(autoTask.getId()!=null){
            dbo.put("_id", autoTask.getId());
        }
        if(null!=autoTask.getPid()){
            dbo.put("pid", autoTask.getPid());
        }
        dbo.put("bussiKey",autoTask.getBussiKey());
        dbo.put("action",autoTask.getAction());
        dbo.put("result",autoTask.getResult());
        dbo.put("createTime", ObjUtils.toLong(autoTask.getCreateTime(), System.currentTimeMillis()));
        dbo.put("activeTime", autoTask.getActiveTime());
        dbo.put("createUser", autoTask.getCreateUser());
        if(null!=autoTask.getRealActiveTime()){
            dbo.put("realActiveTime",autoTask.getRealActiveTime());
        }
        return dbo;
    }

    public static AutoTask castDBObjectToAutoTask(DBObject dbObject){
        AutoTask autoTask = new AutoTask();
        if(dbObject != null) {
            autoTask.setId(ObjUtils.toLong(dbObject.get("_id")));
            autoTask.setBussiKey(ObjUtils.toString(dbObject.get("bussiKey")));
            autoTask.setPid(ObjUtils.toLong(dbObject.get("pid")));
            autoTask.setAction(ObjUtils.toInteger(dbObject.get("action")));
            autoTask.setResult(ObjUtils.toInteger(dbObject.get("result")));
            autoTask.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            autoTask.setActiveTime(ObjUtils.toLong(dbObject.get("activeTime")));
            autoTask.setCreateUser(ObjUtils.toString(dbObject.get("createUser")));
            autoTask.setRealActiveTime(ObjUtils.toLong(dbObject.get("realActiveTime")));
        }
        return autoTask;
    }
}
